package com.lithium.dao;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import com.lithium.common.ActiveIngredient;
import com.lithium.common.Medication;
import com.lithium.common.MedicationCommentTitle;
import com.lithium.mappers.ActiveIngredientRowMapper;
import com.lithium.mappers.MedicationTitleRowMapper;

public class CustomDAO {
	
	private DataSource dataSource;
	
	public DataSource getDataSource() {
		return dataSource;
	}


	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}


	public void create(String firstName, String lastName) {
		JdbcTemplate insert = new JdbcTemplate(dataSource);
		insert.update("INSERT INTO PERSON (FIRSTNAME, LASTNAME) VALUES(?,?)",
				new Object[] { firstName, lastName });
	}
	
	
	@SuppressWarnings("unchecked")
	public List<ActiveIngredient> selectByIngredient(String ingredient){
		JdbcTemplate select = new JdbcTemplate(dataSource);
		return select
				.query(
						"select  med_ingredient_id, substance_active, lang_locale from med_ingredients where substance_active = ? ",
						new Object[] { ingredient },
						new ActiveIngredientRowMapper());
		
		/* 	med_ingredient_id INTEGER primary key default NEXTVAL ('med_ing_seq'),
	substance_active VARCHAR(255),
	lang_locale varchar(10) */
	}
	
	public void createIngredient(String ingredient) {
		JdbcTemplate insert = new JdbcTemplate(dataSource);
		insert.update("INSERT INTO med_ingredients (substance_active, lang_locale) VALUES(?,?)",
				new Object[] { ingredient, "es_mx" });
	}
	
	public void createTitle(String title) {
		JdbcTemplate insert = new JdbcTemplate(dataSource);
		insert.update("INSERT INTO med_title_comments (med_comment_title, lang_locale) VALUES(?,?)",
				new Object[] { title, "es_mx" });
	}
	
	@SuppressWarnings("unchecked")
	public List<MedicationCommentTitle> selectCommentTitleByTitle(String title){
		JdbcTemplate select = new JdbcTemplate(dataSource);
		return select
				.query(
						"select  med_comment_title_id, med_comment_title, lang_locale from med_title_comments where med_comment_title = ? ",
						new Object[] { title },
						new MedicationTitleRowMapper());
		
		/* 	med_ingredient_id INTEGER primary key default NEXTVAL ('med_ing_seq'),
	substance_active VARCHAR(255),
	lang_locale varchar(10) */
	}

	@SuppressWarnings("unchecked")
	public List<Medication> selectMedicationByTitle(String title){
		JdbcTemplate select = new JdbcTemplate(dataSource);
		return select
				.query(
						"select  medication_id, medication_title, medication_format, med_ingredient_id, lang_locale from med_title_comments where med_comment_title = ? ",
						new Object[] { title },
						new MedicationTitleRowMapper());
		
		/* 	med_ingredient_id INTEGER primary key default NEXTVAL ('med_ing_seq'),
	substance_active VARCHAR(255),
	lang_locale varchar(10) */
	}
	

}
